#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void  Playfair(char a[51], char pair[5][5])
{
	int s = 0;
	char* p = a;
	while (*p )
	{
		int m = 0;
		int n = 0;
		int flag = 1;
		int x1 = 0;
		int x2 = 0;
		int y1 = 0;
		int y2 = 0;
		if (*p == *(p + 1))
		{
			printf("%c%c", *p, *(p + 1));
			p = p + 2;
		}
		else if ( *(p + 1) == '\0')
			printf("%c", *p);
		else
		{
			for (m; m < 5; m++)
			{
				for (n = 0; n < 5; n++)
				{
					if (pair[m][n] == *p)
					{
						x1 = m;
						y1 = n;
						flag = 0;
						break;
					}
					if (flag == 0)
						break;
				}
			}
			if (flag)
			{
				printf("%c", *p);
				p++;
				printf("%c", *p);
			}
			if (flag == 0)
			{
				flag = 1;
				m = 0;
				p++;
				for (m; m < 5; m++)
				{
					for (n = 0; n < 5; n++)
					{
						if (pair[m][n] == *p)
						{
							x2 = m;
							y2 = n;
							flag = 0;
							break;
						}
					}
				}
				if (flag)
				{
					printf("%c%c", *(p - 1), *p);
				}
				else
				{
					if (x1 == x2 || y1 == y2)
						printf("%c%c", pair[x2][y2], pair[x1][y1]);
					else
						printf("%c%c", pair[x1][y2], pair[x2][y1]);
				}
			}
		}
		p++;
	}
}
void FIND(char pair[5][5], char alphy[26])
{
	int m = 0;
	int n = 0;
	int x = 0;
		for (m; m <5; m++)
		{
			for (n = 0; n <5; n++)
			{
				for (x=0; x < 26; x++)
				{
					if (alphy[x] == pair[m][n])
						alphy[x] ='0';
				}

			}
		}
}
int main()
{
	char pair[5][5] = { 0 };
	char alphy[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
	gets(pair);
	int i = 0;
	int j = 0;
	for (i; i < 5; i++)
	{
		for (j = 0; j < 5; j++)
		{
			if (pair[i][j] == '\0')
				break;
		}
		if (pair[i][j] == '\0')
			break;
	}
	char* p = &pair[i][j];
	int cd = 25 - i * 5 -j-1;
	int ct = 0;
	FIND(pair, alphy);
	for(ct=0;ct<=cd;ct++)
	{
		for (i = 0; i < 26; i++)
		{
			if (alphy[i] != '0')
			{
				*p = alphy[i];
				alphy[i] = '0';
				break;
			}
		}
			p++;
	}
	char a[51] = { '*'};
	gets(a);
	Playfair(a, pair);
	return 0;
}